// pages/goods/list/index.js
import { reqGoodsList  } from '@/api/goods'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [], // 商品列表数据
    isFinish: false, // 判断数据是否加载完毕
    isLoading: false,
    total: 0,
    requestData: {
      page:1,
      limit:10,
      category1Id:'',
      category2Id: ''
    }
  },

  // 返回上一页，查看其他商品
  gotoBack() {
    wx.navigateBack()
  },
  async getGoodList(){
    this.data.isLoading = true
    const { data } = await reqGoodsList(this.data.requestData)
    this.data.isLoading = false
    this.setData({
      goodsList: [...this.data.goodsList, ...data.records],
      total: data.total
    })
  },
  onReachBottom(){
    //console.log("reach the bottom")
    const { goodsList, total, requestData, isLoading } = this.data
    const { page } = requestData;
    if(isLoading) return
    if(goodsList.length === total){
      this.setData({
        isFinish: true
      })
      return
    }
    this.setData({
      requestData: {
        ...this.data.requestData,
        page: page+1
      }
    })
    this.getGoodList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //console.log("onLoad")
    Object.assign(this.data.requestData, options)
    this.getGoodList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      goodsList: [],
      total: 0,
      isFinish: false,
      requestData:{
        ...this.data.requestData, 
        page: 1
      }
    })
    this.getGoodList()
    wx.stopPullDownRefresh()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
